import { webPrintingJobService } from '../../../src/leaflet/services/WebPrintingJobService';
import { WebPrintingJobParameters } from '../../../src/common/iServer/WebPrintingJobParameters';
import { WebPrintingJobContent } from '../../../src/common/iServer/WebPrintingJobContent';
import { WebPrintingJobLayoutOptions } from '../../../src/common/iServer/WebPrintingJobLayoutOptions';
import { WebPrintingJobScaleBarOptions } from '../../../src/common/iServer/WebPrintingJobScaleBarOptions';
import { WebPrintingJobLittleMapOptions } from '../../../src/common/iServer/WebPrintingJobLittleMapOptions';
import { WebPrintingJobLegendOptions } from '../../../src/common/iServer/WebPrintingJobLegendOptions';
import { WebPrintingJobExportOptions } from '../../../src/common/iServer/WebPrintingJobExportOptions';
import { FetchRequest } from '../../../src/common/util/FetchRequest';

var url = GlobeParameter.webPrintingURL;
describe('leaflet_WebPrintingJobService', () => {
    var originalTimeout;
    beforeEach(() => {
        originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
        jasmine.DEFAULT_TIMEOUT_INTERVAL = 50000;
    });
    afterEach(() => {
        jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
    });

    //创建 Web 打印任务
    it('createWebPrintingJob', (done) => {
        var jobService = webPrintingJobService(url);
        var param = new WebPrintingJobParameters({
            content: new WebPrintingJobContent({
                type: 'WEBMAP',
                url: 'https://www.supermapol.com/web/maps/1887887232/map.json'
            }),
            layoutOptions: new WebPrintingJobLayoutOptions({
                templateName: 'A4_landscape_v2',
                title: '土地利用',
                author: '北京超图软件股份有限公司',
                scaleBarOptions: new WebPrintingJobScaleBarOptions({
                    type: 'BAR',
                    unit: 'METER'
                }),
                littleMapOptions: new WebPrintingJobLittleMapOptions({
                    scale: '0.00012099529215826183',
                    center: { x: '12123228.72886939', y: '2731834.3979613623' }
                }),
                legendOptions: new WebPrintingJobLegendOptions({
                    picAsBase64:
                        ''
                })
            }),
            exportOptions: new WebPrintingJobExportOptions({
                format: 'PDF',
                dpi: 96,
                scale: 0.000060497646079130916
            })
        });
        spyOn(FetchRequest, 'commit').and.callFake((method, testUrl, params, options) => {
            expect(params).not.toBeNull();
            expect(options).not.toBeNull();
            if (testUrl === url + '/jobs') {
              return Promise.resolve(new Response(JSON.stringify(createWebPringintJobResultJson)));
            }
            return Promise.resolve(new Response(JSON.stringify(getLayoutsResultJson)));
        });
        jobService.createWebPrintingJob(null);
        jobService.createWebPrintingJob(param, (serviceResult) => {
          try {
              expect(jobService).not.toBeNull();
              expect(serviceResult).not.toBeNull();
              expect(serviceResult.type).toEqual('processCompleted');
              expect(serviceResult.result[0].resourceConfigID).not.toBeNull();
              expect(serviceResult.result[0].path).toEqual(url + '/jobs/' + serviceResult.result[0].resourceConfigID);
              expect(serviceResult.result[0].name).toEqual(serviceResult.result[0].resourceConfigID);
              done();
          } catch (e) {
              console.log("'createWebPrintingJob'案例失败" + e.name + ':' + e.message);
              expect(false).toBeTruthy();
              jobService.destroy();
              done();
          }
      });
    });

    it('getPrintingJob', (done) => {
        var jobId = 'e3ff26fa-a0b5-46d3-ad4a-096611a59c03@9fd5defe-d77d-4e17-967e-643c4f34d67e';
        var jobService = webPrintingJobService(url);
        spyOn(FetchRequest, 'commit').and.callFake((method, testUrl) => {
            expect(method).toBe('GET');
            expect(testUrl).toBe(url + `/jobs/${jobId}`);
            return Promise.resolve(new Response(JSON.stringify(getPrintingJobResultJson)));
        });
        jobService.getPrintingJob(jobId, (serviceResult) => {
            try {
                expect(serviceResult).not.toBeNull();
                expect(serviceResult.type).toBe('processCompleted');
                expect(serviceResult.result).not.toBeNull();
                expect(serviceResult.result.id).toEqual(jobId);
                expect(serviceResult.result.status).toEqual('FINISHED');
                done();
            } catch (exception) {
                expect(false).toBeTruthy();
                console.log("getPrintingJob'案例失败：" + exception.name + ':' + exception.message);
                jobService.destroy();
                done();
            }
        });
    });

//     it('getPrintingJob promise', (done) => {
//       var jobId = 'e3ff26fa-a0b5-46d3-ad4a-096611a59c03@9fd5defe-d77d-4e17-967e-643c4f34d67e';
//       var jobService = webPrintingJobService(url);
//       spyOn(FetchRequest, 'commit').and.callFake((method, testUrl) => {
//           expect(method).toBe('GET');
//           expect(testUrl).toBe(url + `/jobs/${jobId}`);
//           return Promise.resolve(new Response(JSON.stringify(getPrintingJobResultJson)));
//       });
//       jobService.getPrintingJob(jobId).then((serviceResult) => {
//         try {
//             expect(serviceResult).not.toBeNull();
//             expect(serviceResult.type).toBe('processCompleted');
//             expect(serviceResult.result).not.toBeNull();
//             expect(serviceResult.result.id).toEqual(jobId);
//             expect(serviceResult.result.status).toEqual('FINISHED');
//             done();
//         } catch (exception) {
//             expect(false).toBeTruthy();
//             console.log("getPrintingJob'案例失败：" + exception.name + ':' + exception.message);
//             jobService.destroy();
//             done();
//         }
//     });
//   });

    // 获取 Web 打印模板列表
    it('getLayoutTemplates', (done) => {
        var jobService = webPrintingJobService(url);
        spyOn(FetchRequest, 'commit').and.callFake((method, testUrl) => {
            expect(method).toBe('GET');
            expect(testUrl).toBe(url + '/layouts');
            return Promise.resolve(new Response(JSON.stringify(getLayoutsResultJson)));
        });
        jobService.getLayoutTemplates((serviceResult) => {
            try {
                expect(jobService).not.toBeNull();
                expect(serviceResult).not.toBeNull();
                expect(serviceResult.type).toEqual('processCompleted');
                var result = serviceResult.result;
                expect(result.length).toBeGreaterThan(0);
                for (var i = 0; i < result.length; i++) {
                    expect(result[i].templateName).not.toBeNull();
                    expect(result[i].layoutOptions).not.toBeNull();
                }
                done();
            } catch (e) {
                console.log("'getLayoutTemplates'案例失败" + e.name + ':' + e.message);
                expect(false).toBeTruthy();
                jobService.destroy();
                done();
            }
        });
    });
});
